package com.sunnyweather.android.ui.place

import androidx.lifecycle.*
import com.sunnyweather.android.logic.Repository
import com.sunnyweather.android.logic.model.Place
import com.sunnyweather.android.logic.util.LogUtil

class PlaceViewModel:ViewModel() {

    //UI层搜索框的数据->查询关键字
    private val searchLiveData=MutableLiveData<String>()

    //监听查询关键字并实时发起网络请求获取结果
    val placeLiveData:LiveData<Result<List<Place>>> = Transformations.switchMap(searchLiveData){ query->
         Repository.queryPlaces(query)
    }

    //用来缓存UI层数据
    val placeList= mutableListOf<Place>()





    //供UI层调用的函数（用来设置查询的关键字）
    fun queryPlaces(query:String){
        searchLiveData.value=query
    }

    fun setPlace(place:Place){
        LogUtil.d("PlaceViewModel.setPlace",place.toString())
        Repository.setPlace(place)
    }

    fun getRestoredPlace()=Repository.getRestoredPlace()

    fun isPlaceStored()=Repository.isStored()

    fun addPlace(place: Place)=Repository.addPlace(place)

    fun removePlace(key:String)=Repository.removePlace(key)

    fun getPlaceFromList(key:String)=Repository.getPlaceFromList(key)

    fun getPlaceIndex()=Repository.getPlaceIndex()

}